<?php

namespace App\Http\Requests;

use App\Models\ManageAdminModel;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class AdminRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {

        if (request()->route()->hasParameter('admin_name')){
            $admin_name = request()->route()->parameter('admin_name');
            $id = ManageAdminModel::where('mgr_name',$admin_name)->value('mgr_id');
            return [
                'admin_name'=>['required','alpha_start', 'unique:manage_admins,mgr_name,'.$id.',mgr_id'],
                'admin_email'=>'required|email',
                'admin_mobile'=>'required|mobile',
            ];
        }else{
            return [
                'admin_name'=>'required|alpha_start|unique:manage_admins,mgr_name',
                'admin_email'=>'required|email',
                'admin_mobile'=>'required|mobile',
                'login_pwd'=>'required|between:6,24',
                'confirm_login_pwd'=>'required|same:login_pwd',
                'safe_pwd'=>'required|between:6,24',
                'confirm_safe_pwd'=>'required|same:safe_pwd',
                'status'=>'required|boolean',
            ];
        }

    }

    public function messages()
    {
        return [
            'required'=>':attribute不能为空',
            'alpha_start'=>':attribute必须以字母开头仅包含字母数字或下划线',
            'email'=>':attribute格式不正确',
            'mobile'=>':attribute格式不正确',
            'between'=>':attribute长度必须在:min到:max之间',
            'pwd_rule'=>':attribute必须至少包含一个大写字母，一个小写字母,一个特殊字符',
            'same'=>':attribute必须和:field相同',
            'unique'=>':attribute不能重复',
        ];
    }

    public function attributes()
    {
        return [
            'admin_name'=>'管理员账号',
            'admin_email'=>'邮箱',
            'admin_mobile'=>'手机号',
            'login_pwd'=>'登录密码',
            'confirm_login_pwd'=>'确认登录密码',
            'safe_pwd'=>'安全密码',
            'confirm_safe_pwd'=>'确认安全密码',
            'status'=>'状态',
        ];
    }
}
